package cn.lyh97.estore.easystoremanager.Fragments;


import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.List;

import cn.lyh97.estore.easystoremanager.Activities.MainActivity;
import cn.lyh97.estore.easystoremanager.Classes.GoodsAdapter;
import cn.lyh97.estore.easystoremanager.Classes.GoodsItem;
import cn.lyh97.estore.easystoremanager.Classes.SqlHelper;
import cn.lyh97.estore.easystoremanager.R;


public class GoodsFragment extends Fragment {
    SqlHelper sqlHelper;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_goods, container, false);
        sqlHelper = new SqlHelper(this.getActivity());
        List<GoodsItem> goodsList;
        goodsList = sqlHelper.getAllGoodsItem();
        GoodsAdapter goodAdapter = new GoodsAdapter(this.getActivity(), goodsList);
        ListView listView = (ListView) view.findViewById(R.id.goodsFragmentListView);
        listView.setAdapter(goodAdapter);
//<editor-fold listView.setOnClickListener>

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                FragmentTransaction transaction = MainActivity.fragmentManager.beginTransaction();
                transaction.hide(MainActivity.nowFragment);
                transaction.show(MainActivity.itemFragment);
                MainActivity.nowFragment = MainActivity.itemFragment;
                transaction.commit();
                List<GoodsItem> goodsList;
                goodsList = sqlHelper.getAllGoodsItem();
                GoodsItem item = sqlHelper.getGoodsItemByID(goodsList.get(position).getID());

                MainActivity.updateItemFragment(MainActivity.itemFragment,item);
            }
        });

        listView.setOnItemLongClickListener(
                new AdapterView.OnItemLongClickListener() {
                    @Override
                    public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
                        AlertDialog.Builder builder = new AlertDialog.Builder(GoodsFragment.this.getActivity());
                        builder.setView(R.layout.main_dialog_delete)
                                .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        List<GoodsItem> goodsList;
                                        goodsList = sqlHelper.getAllGoodsItem();

                                        sqlHelper.deleteGoodsItemByID(goodsList.get(position).getID());

                                        Toast.makeText(GoodsFragment.this.getActivity(), "删除成功！", Toast.LENGTH_SHORT).show();
                                    }
                                })
                                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        Toast.makeText(GoodsFragment.this.getActivity(), "取消", Toast.LENGTH_SHORT).show();
                                    }
                                });
                        builder.create().show();

                        return false;
                    }
                });
//</editor-fold>

//        View view = inflater.inflate(R.layout.fragment_goods, container, false);
//        List<GoodsItem> goodsList;
//        SqlHelper sqlHelper = new SqlHelper(getActivity());
//        goodsList = sqlHelper.getAllGoodsItem();
//        GoodsAdapter goodAdapter = new GoodsAdapter(getActivity(), goodsList);
//        ListView listView = (ListView) getActivity().findViewById(R.id.goodsFragmentListView);
//        listView.setAdapter(goodAdapter);
        return view;
    }
}
